import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author wcj
 * @Date 2021/6/1 15:07
 * @Version 1.0
 */
public class DirectMemoryOOM {

    private static final int _1MB = 1024 * 1024;

    /**
     * 直接内存溢出的判断：Dump文件信息很少并且代码中直接或者间接的使用了NIO
     * @param args
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalAccessException {
        Field declaredField = Unsafe.class.getDeclaredFields()[0];
        declaredField.setAccessible(true);
        Unsafe unsafe = (Unsafe) declaredField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }

    }
}
